package com.example.screenrecorder;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {
    
    private static final String PREFS_NAME = "screen_recorder_settings";
    private static final String KEY_VIDEO_QUALITY = "video_quality";
    private static final String KEY_AUDIO_ENABLED = "audio_enabled";
    private static final String KEY_SHOW_TOUCHES = "show_touches";
    private static final String KEY_RECORDING_DURATION = "recording_duration";
    
    private SharedPreferences preferences;
    
    private Switch switchAudio;
    private Switch switchShowTouches;
    private SeekBar seekBarQuality;
    private SeekBar seekBarDuration;
    private TextView tvQualityValue;
    private TextView tvDurationValue;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        
        preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        
        initViews();
        setupClickListeners();
        loadSettings();
    }
    
    private void initViews() {
        switchAudio = findViewById(R.id.switch_audio);
        switchShowTouches = findViewById(R.id.switch_show_touches);
        seekBarQuality = findViewById(R.id.seekbar_quality);
        seekBarDuration = findViewById(R.id.seekbar_duration);
        tvQualityValue = findViewById(R.id.tv_quality_value);
        tvDurationValue = findViewById(R.id.tv_duration_value);
    }
    
    private void setupClickListeners() {
        // 音频开关
        switchAudio.setOnCheckedChangeListener((buttonView, isChecked) -> {
            preferences.edit().putBoolean(KEY_AUDIO_ENABLED, isChecked).apply();
        });
        
        // 显示触摸开关
        switchShowTouches.setOnCheckedChangeListener((buttonView, isChecked) -> {
            preferences.edit().putBoolean(KEY_SHOW_TOUCHES, isChecked).apply();
        });
        
        // 质量滑块
        seekBarQuality.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                String quality = getQualityText(progress);
                tvQualityValue.setText(quality);
                preferences.edit().putInt(KEY_VIDEO_QUALITY, progress).apply();
            }
            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        // 录制时长滑块
        seekBarDuration.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                String duration = getDurationText(progress);
                tvDurationValue.setText(duration);
                preferences.edit().putInt(KEY_RECORDING_DURATION, progress).apply();
            }
            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
    }
    
    private void loadSettings() {
        // 加载音频设置
        boolean audioEnabled = preferences.getBoolean(KEY_AUDIO_ENABLED, true);
        switchAudio.setChecked(audioEnabled);
        
        // 加载显示触摸设置
        boolean showTouches = preferences.getBoolean(KEY_SHOW_TOUCHES, false);
        switchShowTouches.setChecked(showTouches);
        
        // 加载质量设置
        int quality = preferences.getInt(KEY_VIDEO_QUALITY, 2); // 默认中等质量
        seekBarQuality.setProgress(quality);
        tvQualityValue.setText(getQualityText(quality));
        
        // 加载录制时长设置
        int duration = preferences.getInt(KEY_RECORDING_DURATION, 0); // 默认无限制
        seekBarDuration.setProgress(duration);
        tvDurationValue.setText(getDurationText(duration));
    }
    
    private String getQualityText(int progress) {
        switch (progress) {
            case 0: return "低质量 (2 Mbps)";
            case 1: return "中等质量 (4 Mbps)";
            case 2: return "高质量 (8 Mbps)";
            case 3: return "超高质量 (12 Mbps)";
            default: return "高质量 (8 Mbps)";
        }
    }
    
    private String getDurationText(int progress) {
        switch (progress) {
            case 0: return "无限制";
            case 1: return "5分钟";
            case 2: return "10分钟";
            case 3: return "30分钟";
            case 4: return "60分钟";
            default: return "无限制";
        }
    }
    
    // 获取设置值的静态方法
    public static boolean isAudioEnabled(SharedPreferences prefs) {
        return prefs.getBoolean(KEY_AUDIO_ENABLED, true);
    }
    
    public static boolean isShowTouchesEnabled(SharedPreferences prefs) {
        return prefs.getBoolean(KEY_SHOW_TOUCHES, false);
    }
    
    public static int getVideoQuality(SharedPreferences prefs) {
        return prefs.getInt(KEY_VIDEO_QUALITY, 2);
    }
    
    public static int getRecordingDuration(SharedPreferences prefs) {
        return prefs.getInt(KEY_RECORDING_DURATION, 0);
    }
}
